/**
 * this method will implement the controller of the network manager.
 * apart of using all of the lot manager's options he will br able to take snapshot of any lot,
 * and to order statistical reports.
 * 
 * @author Gil Levinson
 * @author Boris Fradlis
 * @author Yogev Levi
 * @author Hila Porat
 * 
 */
package control;

import java.awt.Desktop;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.Vector;
import javax.swing.JFrame;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.GraphicReport;
import vcpClasses.ParkingLocation;

// Start of networkManagerController *****************************************

public class CurrentSituationController extends AbstractController {

	private SystemTypes sysType;
	private boolean returned = false;
	
	
	
	// constructor *********************************************************
	
	/**
	 * constructor for this controller instance
	 * @param sysType - the object we'll control
	 */
	public CurrentSituationController(SystemTypes sysType) {
		this.sysType = sysType;
		this.returned = false;
	}

	// controller methods *****************************************************
	
	public SystemTypes getSnapshot(SystemTypes myType, String TypeOfReport)
	{
		returned = false;
		myType.setSysMode(SystemMode.ISSUE_REPORTS);
		boolean flag = this.SendToServer(myType);
		while (!returned){}
		if (!flag)
		{
			switch (this.sysType.getSysMode())
			{
				case DB_CON_ERR:
					this.sysType.setObj("Couln't operate the request agianst the DB!");
					break;
				case EMPTY_RES:
					this.sysType.setObj("The lot wasn't Initialized!!");
					break;					
			}
		}
		else
			if (TypeOfReport.equals("PDF"))
				try {
					this.createPdfReport(this.sysType);
				} catch (Exception e) {
					this.sysType.setSysMode(SystemMode.SYS_ERR);
					this.sysType.setObj("Problem creating PDF file!");
					e.printStackTrace();
				}
			else
				this.createGraphicReport(this.sysType);
		while (!returned){}
		return this.sysType;
	}
	
	/**
	 * This methods creates and saves new PDF snapshot of given lot.
	 * the report is saved in 'c:\temp' directory 
	 * @param myType  the lot's parking spaces 
	 * @throws Exception - when dealing with the pdf unsuccesful
	 */
	
	public void createPdfReport(SystemTypes myType) throws Exception
	{
		int i,j,k, index=0, free =0 , taken=0, damaged=0, maintenance=0,saved =0;
		File file = null;
		Vector<ParkingLocation> vec = (Vector<ParkingLocation>) sysType.getObj();
		ParkingLocation location = new ParkingLocation();
		location = vec.lastElement();
		int spaces = location.getParkingSpaceNumber();
		Document document = new Document(PageSize.A4,50,50,50,50);
		String pathToFile = "C:\\temp\\";
		String fileName = "SnapShot.pdf";
		boolean createDir = new File(pathToFile).mkdir();
		file = new File(pathToFile + fileName);
		PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("C:\\temp\\SnapShot.pdf"));
		document.open();
		String title = "SnapShot of parking lot no. "+location.getParkingLotNumber();
		document.addTitle(title);
		Paragraph space = null;
		for (i=0; i<3; i++)
		{
			Paragraph chapterTitle =  new Paragraph("Floor no. "+(i+1), FontFactory.getFont(FontFactory.HELVETICA, 18, Font.UNDERLINE, new BaseColor(0, 100, 100)));
			Chapter chapter = new Chapter(chapterTitle,1);
			chapter.setNumberDepth(0);
			for (j=0; j<3; j++)
			{
				Paragraph row = new Paragraph("Row no. "+(j+1), FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new BaseColor(0, 0, 0)));
				chapter.add(row);
								
				for (k=0; k<spaces; k++)
				{
					space = new Paragraph();
					location = vec.get(index);
					switch (location.getParkingSpaceStatus())
					{
						case "FREE":
							space = new Paragraph("space no. "+(k+1)+", Status: FREE", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new BaseColor(0, 255, 0)));
							free++;
							break;
						case "OCCUPIED":
							space = new Paragraph("space no. "+(k+1)+", Status: OCCUPIED", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new BaseColor(0, 0, 255)));
							taken++;
							break;
						case "SAVED":
							space = new Paragraph("space no. "+(k+1)+", Status: SAVED", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new BaseColor(0, 0, 255)));
							saved++;
							break;
						case "MAINTENANCE":
							space = new Paragraph("space no. "+(k+1)+", Status: MAINTENANCE", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new BaseColor(255, 0, 0)));
							maintenance++;
							break;
						case "DAMAGED":
							space = new Paragraph("space no. "+(k+1)+", Status: DAMAGED", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new BaseColor(255, 0, 0)));
							damaged++;
							break;
						default:
							space = new Paragraph("space no. "+(k+1)+", Status: UNKNOWN", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new BaseColor(0, 0, 0)));
							break;
					}
					index++;
					chapter.add(space);
				}				
			}
			document.add(chapter);
		}
		space = new Paragraph("Summary:", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLDITALIC, new BaseColor(0, 0, 0)));
		Chapter chapter = new Chapter(space,1);
		chapter.setNumberDepth(0);
		space = new Paragraph("Total of free parking spaces: " + free, FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new BaseColor(0, 255, 0)));
		chapter.add(space);
		space = new Paragraph("Total of occupied parking spaces: " + taken, FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new BaseColor(0, 0, 255)));
		chapter.add(space);
		space = new Paragraph("Total of saved parking spaces: " + saved, FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new BaseColor(0, 0, 255)));
		chapter.add(space);
		space = new Paragraph("Total of maintenance parking spaces: " + maintenance, FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new BaseColor(255, 0, 0)));
		chapter.add(space);
		space = new Paragraph("Total of damaged parking spaces: " + damaged, FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new BaseColor(255, 0, 0)));
		chapter.add(space);
		document.add(chapter);
		document.close();

		if (Desktop.isDesktopSupported())
		{
			Desktop.getDesktop().open(file);
		}
		
	}
	
	/**
	 * this method will initiate graphic snapshot of the given parking lot
	 * @param myType - the parking lot to snap
	 */
	
	public void createGraphicReport(SystemTypes myType)
	{
		JFrame f = new JFrame();
		f.setTitle("VCP Client v1.0");
		f.setIconImage(Toolkit.getDefaultToolkit().getImage(VCP.class.getResource("/img/Icon.png")));
		f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		GraphicReport gr = new GraphicReport(myType);
		f.add(gr);
		f.setSize(850,500);
		f.setVisible(true);
	}
	
	

	
	// Interface override method **********************************************

	@Override
	public boolean SendToServer(Serializable obj) {
		VCP.client.send(obj);
		sysType = (SystemTypes) VCP.client.getResult();
		if (sysType.getSysMode() != SystemMode.OK)
		{
			setReturned(true);
			return false;
		}
		setReturned(true);
		return true;
	}
	
	// Getters and Setters *******************************************************


	/**
	 * @return the returned
	 */
	public boolean isReturned() {
		return returned;
	}


	/**
	 * @param returned the returned to set
	 */
	public void setReturned(boolean returned) {
		this.returned = returned;
	}
}
